<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	cfc('core')->checklogin();
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('nickname','mobile'));	
	$post=array(
		'totalprice'=>0,
		'price'=>0.00,
		'username'=>$member['nickname'],
		'mobile'=>$member['mobile'],
		'diyfields'=>array(),
		'total'=>1,
		'type'=>0
	);
	$days=array();
	for($i=0;$i<15;$i++){
		$days[]=date("Y-m-d",strtotime("+".$i." day"));
	}
	$return['days']=$days;
	$daytime=date('Y-m-d');
	$start = mktime(8, 0);
	$end = mktime(20, 0);
	$times=array();
	for($i = $start; $i < $end;) {
		$times[]=date('H:i', $i);
		$i += 3600;
	}
	$return['times']=$times;
	$goods=array();
	if($_RQ['serve_id']){
		$fuwu=pdo_get('jy_djhome_serve',array('id'=>intval($_RQ['serve_id']),'uniacid'=>$_SESSION['uniacid']),array('id','title','price','times'));
		$fuwu['total']=1;
		$goods[]=$fuwu;
	}else{
		$goods=$this->session('goods');
	}
	if($_RQ['worker_id']){
		$worker=pdo_get('jy_djhome_worker',array('id'=>intval($_RQ['worker_id']),'uniacid'=>$_SESSION['uniacid']));
		$worker['latlng']=json_decode($worker['latlng'],true);
	}
	if(!$worker['id']){
		exi('请选择技师！','error');
	}
	if(!count($goods)){
		exi('请选择项目！','error');
	}
	$totalprice=0;
	foreach($goods as $k=>$gv){
		if($gv['total']<=0){
			unset($goods[$k]);
			continue;
		}
		$totalprice +=$gv['price']*$gv['total'];
	}
	$address=pdo_get('core_address',array('isdel'=>0,'isdefault'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if(!$address){
		$address=pdo_get('core_address',array('isdel'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	}
	$address['pcd']=json_decode($address['pcd'],true);
	$post=array(
		'goods'=>$goods,
		'worker_id'=>$worker['id'],
		'totalprice'=>price($totalprice,'int'),
	);
	$post['address']=$address;
	$post['addressid']=$address['id'];
	$post['chetype']=2;
	$getchefee=$this->model('model')->chefee($post['chetype'],$post['address'],$worker['latlng']);
	$post['chefee']=$getchefee['chefee'];
	$post['juli']=$getchefee['juli'];
	$post['day']=date('Y-m-d');
	$post['time']=date('H:i',time()+1800);
	$return['post']=$post;
	$return['worker']=$worker;
	$return['nowmonthday']=date('Y-m-d');
	$return['nowtime']=time();
	$return['buyxieyi']=$this->plugin_setting('xieyi');
	set_html('title','下单');
	exi($return);
}
if($_RQ['op']=='get_chefee'){
	$chetype=intval($_RQ['chetype']);
	$addressid=intval($_RQ['addressid']);
	if(!$addressid){
		exi('请先选择地址！','error');
	}
	$worker=pdo_get('jy_djhome_worker',array('id'=>intval($_RQ['worker_id']),'uniacid'=>$_SESSION['uniacid']));
	$worker['latlng']=json_decode($worker['latlng'],true);
	$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$addressid));
	$address['pcd']=json_decode($address['pcd'],true);
	$getchefee=$this->model('model')->chefee($chetype,$address,$worker['latlng']);
	exi($getchefee);
}
if($_RQ['op']=='get_work_time'){
	$worker=pdo_get('jy_djhome_worker',array('id'=>intval($_RQ['worker_id']),'uniacid'=>$_SESSION['uniacid']));
	$onlinetime=json_decode($worker['onlinetime'],true);
	$daytime=date('Y-m-d');
	if($onlinetime['start']){
		$start=$daytime.' '.$onlinetime['start'];
		$start=strtotime($start);
	}else{
		$start = mktime(8, 0);
	}
	if($onlinetime['end']){
		$end=$daytime.' '.$onlinetime['end'];
		$end=strtotime($end);
	}else{
		$end = mktime(22, 0);
	}
	$times=array();
	for($i = $start; $i < $end;) {
			$times[] = array(
						'start' => date('H:i', $i),
						'end' => date('H:i', $i + 1800),
						'timestamp' => $i + 1800,
						'fee' => 0,
						'timev' =>date('H:i', $i).'~'.date('H:i', $i + 1800),
			);
			$i += 1800;
	}
	$work_time=str_array($store['work_time']);
	//门店营业时间
	//设置日期
	$data=array();
	$weekarray=array('周日','周一','周二','周三','周四','周五','周六'); //先定义一个数组
	for($i=0;$i<7;$i++){
		$weekid=date("w",strtotime(date("Y-m-d",strtotime("+".$i." day"))));
		if(count($work_time)){
			$iswork=strpos($store['work_time'],';'.$weekid.';')?1:0;
		}else{
			$iswork=1;
		}
		$data[]=array(
			'riqi'=>date("m-d",strtotime("+".$i." day")),
			'day'=>date("Y-m-d",strtotime("+".$i." day")),
			'xinq'=>$i?$weekarray[$weekid]:'今天',
			'iswork'=>$iswork,
		);
	}
	$return=array('days'=>$data,'hours'=>$times);
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$pid = $postdata['pid'];
	$groupid = $postdata['groupid'];
	$buyxieyi=$this->plugin_setting('xieyi');
	if(!$postdata['checked'] && $buyxieyi['isopen']){
		//exi('请先阅读协议！','error');
	} 
	if(!$postdata['username']){
		//exi('请输入联系人姓名','error');
	}
	if(!$postdata['mobile']){
		//exi('请输入联系人手机号码','error');
	}  
	if(!preg_match("/^1[345678]{1}\d{9}$/",$postdata['mobile'])){  	 
		//exi('请输入正确的手机号码','error');
	}
	if(!$postdata['addressid']){
		exi('请选择上门地址！！','error');
	}
	$postdata['address']=$postdata['address']?:$postdata['addressinfo']['address'];
	if(!$postdata['address']){
		exi('请选择上门地址！','error');
	}
	if(!$postdata['day'] || !$postdata['time']){
		exi('请选择上门时间！','error');
	}
	if(!count($postdata['goods'])){
		exi('请选择项目！','error');
	}
	$price1=0;
	foreach($postdata['goods'] as $k=>$gv){
		if($gv['total']<=0){
			unset($postdata['goods'][$k]);
			continue;
		}
		$price1 +=$gv['price']*$gv['total'];
	}
	$totalprice=$price1+$postdata['chefee'];
	$postdata['serve_time'] = $postdata['day'].' '.$postdata['time'];
	$postdata['serve_time']=strtotime($postdata['serve_time']);
	$postdata['price']=$totalprice;
	$postdata['price1']=$price1;
	$id=$this->model('order')->create($postdata);
	$this->session('goods','');
	if($totalprice>0){
			$params=array(
					'id'=>$id,
					'title'=>'服务订单',
					'fee'=>$totalprice,
					'plugin'=>'jy_djhome',
					//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
					'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
					'back_url'=>'jy_djhome/book/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
					'success_url'=>'jy_djhome/order/',
					'error_url'=>'jy_djhome/order/',
			);
			$payid=cfc('pay')->create_pay_order($params);
			$this->pdo_update('order',array('pay_id'=>$payid),array('id'=>$id));
			$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
			);
			exi($res);
	}else{
			$payresult=array('fee'=>0);
			$order=$insert;
			$order['id']=$id;
			$this->model('model')->payresult($order,$payresult,'',$yuejiemid);
			$this->model('notice')->notice_fuwuorder($order);	
			exi('预约成功！','','jy_djhome/order/');
	}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_djhome_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	$this->model('order')->order_pay($order['id'],$params,1);
	exi('支付成功','success','jy_djhome/order/list/status=1&id='.$order['id']);
}